package com.getpebble.android.framework.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BadParcelableException;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.ObjectUtil;
import com.getpebble.android.framework.comm.MessageRouter;
import com.getpebble.android.framework.endpoint.EndpointRequest;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.protocol.EndpointId;
import java.util.Set;

public class MusicReceiver extends BroadcastReceiver {
    private static final String TAG = MusicReceiver.class.getSimpleName();
    private static MusicInfo sMusicInfo = new MusicInfo();

    public static class MusicInfo {
        public String mAlbum = "";
        public String mArtist = "";
        public String mTrack = "";
    }

    public void onReceive(Context context, Intent intent) {
        String artistExtra = null;
        try {
            String artist;
            String track;
            String album;
            if (intent.hasExtra("artist")) {
                artistExtra = intent.getStringExtra("artist");
            } else if (intent.hasExtra("ARTIST_NAME")) {
                artistExtra = intent.getStringExtra("ARTIST_NAME");
            } else if (intent.hasExtra("com.amazon.mp3.artist")) {
                artistExtra = intent.getStringExtra("com.amazon.mp3.artist");
            }
            String trackExtra = null;
            if (intent.hasExtra("track")) {
                trackExtra = intent.getStringExtra("track");
            } else if (intent.hasExtra("TRACK_NAME")) {
                trackExtra = intent.getStringExtra("TRACK_NAME");
            } else if (intent.hasExtra("com.amazon.mp3.track")) {
                trackExtra = intent.getStringExtra("com.amazon.mp3.track");
            }
            String albumExtra = null;
            if (intent.hasExtra("album")) {
                albumExtra = intent.getStringExtra("album");
            } else if (intent.hasExtra("ALBUM_NAME")) {
                albumExtra = intent.getStringExtra("ALBUM_NAME");
            } else if (intent.hasExtra("com.amazon.mp3.album")) {
                albumExtra = intent.getStringExtra("com.amazon.mp3.album");
            }
            if (artistExtra == null) {
                artist = "";
            } else {
                artist = artistExtra;
            }
            if (trackExtra == null) {
                track = "";
            } else {
                track = trackExtra;
            }
            if (albumExtra == null) {
                album = "";
            } else {
                album = albumExtra;
            }
            if (isDuplicateNotification(artist, album, track)) {
                StringBuilder sb = new StringBuilder();
                sb.append("Received duplicate music updated intent; artist=").append(artist).append(" album=").append(album).append(" track=").append(track);
                Trace.debug(TAG, sb.toString());
                return;
            }
            MusicInfo musicInfo = new MusicInfo();
            musicInfo.mArtist = artist;
            musicInfo.mAlbum = album;
            musicInfo.mTrack = track;
            sMusicInfo = musicInfo;
            doMusicChanged();
        } catch (BadParcelableException e) {
            Trace.warning(TAG, "Error receiving music broadcast");
        }
    }

    private boolean isDuplicateNotification(String artist, String album, String track) {
        return ObjectUtil.nullCheckEquals(artist, sMusicInfo.mArtist) && ObjectUtil.nullCheckEquals(album, sMusicInfo.mAlbum) && ObjectUtil.nullCheckEquals(track, sMusicInfo.mTrack);
    }

    public static MusicInfo getMusicInfo() {
        return sMusicInfo;
    }

    public void doMusicChanged() {
        EndpointRequest musicRequest = new EndpointRequest(EndpointId.MUSIC_CONTROL, EndpointAction.UPDATE_MUSIC_NOW_PLAYING);
        for (MessageRouter router : getAllRouters()) {
            router.onRequest(musicRequest, null);
        }
    }

    protected Set<MessageRouter> getAllRouters() {
        return MessageRouter.getAllRouters();
    }
}
